/* $Id: BoundLabel.java,v 1.2 2007/09/07 06:51:21 gigabass Exp $
 *
 * Copyright (c) Guido Ludwig. All rights reserved.
 * Use is subject to license terms.
 */
package de.guidoludwig.af;

import java.awt.Font;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JLabel;

import com.jgoodies.binding.value.ValueModel;

/**
 * A JLabel bound to a value model.
 * 
 * @author Guido
 * 
 */
public class BoundLabel extends JLabel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public static JLabel createHeaderLabel(ValueModel model) {
		JLabel label = new BoundLabel(model);
		Font font = label.getFont();
		Font headerFont = font.deriveFont(font.getStyle(), 2 * font.getSize());
		label.setFont(headerFont);
		return label;
	}
	
	private ValueModel model;

	public BoundLabel(ValueModel model) {
		super("");
		this.model = model;
		this.model.addValueChangeListener(new TextUpdater());
		updateText();
	}

	private void updateText() {
		setText(model.getValue() == null ? "" : model.getValue().toString());
	}

	private class TextUpdater implements PropertyChangeListener {
		public void propertyChange(PropertyChangeEvent evt) {
			updateText();
		}
	}
}
